//
//  XMLReader.m
//  KOFY
//
//  Created by Warren Holybee on 12/1/10.
//  Copyright 2010 Warren Holybee. All rights reserved.
//
// Not implemented, other than creating fake data for the survey
// XMLParser will parse XML from BIM, or another source for the questions
// It will eventually also submit (in the form of XML or something else)
// the answers back to BIM



#import "XMLReader.h"
#import "defines.h"

@implementation XMLReader
@synthesize survey = _survey;
@synthesize KofyClubURL = _KofyClubURL;
@synthesize TwitterFeed = _TwitterFeed;
@synthesize newQuestion = _newQuestion;
@synthesize newQuestionString = _newQuestionString;
@synthesize newAnswerString = _newAnswerString;


-(void)initWithURL:(NSString *)urlString {
	inQuestion = NO;
	elementType = kUnknown;
	NSURL *url = [[NSURL alloc] initWithString:urlString];
	xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
	[xmlParser setDelegate:self];
	[xmlParser parse];
	[url release];
	
	[super init];
}




- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
	attributes:(NSDictionary *)attributeDict {

	NSLog(@"Processing Element: %@", elementName);
	
	if([elementName isEqualToString:@"survey"]) {
		//Initialize the array.
		self.survey = [[NSMutableArray alloc] initWithCapacity:4];

	}
	else if([elementName isEqualToString:@"question"]) {
		[self.newQuestion.answers release];
		[self.newQuestion release];
		self.newQuestion = [[Question alloc] init ];
		self.newQuestion.answers = [[NSMutableArray alloc] initWithCapacity:2];
		inQuestion = YES;
		elementType = kQuestion;
		NSLog (@"Found question");
		
	} else if ([elementName isEqualToString:@"text"]) {
		[self.newQuestionString release];
		self.newQuestionString = [[NSMutableString alloc] init ];
		elementType = kText;
		NSLog (@"Found question Text");
		
	}	else if ([elementName isEqualToString:@"answer"]) {
		[self.newAnswerString release];
		self.newAnswerString = [[NSMutableString alloc] init ];
		elementType = kAnswer;
		NSLog (@"Found Answer");
		
	}	
	

		
	
	}


- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
	NSLog(@"end of element %@",elementName);

	if([elementName isEqualToString:@"survey"])
	{}
	
	if([elementName isEqualToString:@"question"]) {  // end of question, add to to survey
		
		[self.survey addObject:self.newQuestion];
		inQuestion = NO;
		elementType = kUnknown;
	}
	
	
	if([elementName isEqualToString:@"text"]) {  // end of question text, add to to question
		NSLog(@"storing question text");
		self.newQuestion.question = self.newQuestionString;
		elementType = kUnknown;
	}
	
	if([elementName isEqualToString:@"answer"]) {  // end of answer text, add to to question answers
		NSLog(@"storing answer text: %@",self.newAnswerString);
		[self.newQuestion.answers addObject:self.newAnswerString];
		elementType = kUnknown;
	}
	
}


// parse chars in element

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
	
	if (elementType == kUnknown) return;

	if (elementType == kText) {
		[self.newQuestionString appendString:string];
	}


	if (elementType == kAnswer) {
		[self.newAnswerString appendString:string];
	}
	
	
	NSLog(@"Processing Value: %@", string);
	
}


-(void)createFakeData {
	self.survey = [[NSMutableArray alloc] initWithCapacity:4];
	for (int i=1; i < 5; i++) {
		Question *question = [[Question alloc] init];
		question.question =[NSString stringWithFormat:@"This is question %d",i];
		NSString *a1 = @"This is answer 1";
		NSString *a2 = @"This is answer 2";
		NSString *a3 = @"This is answer 3";
		NSString *a4 = @"This is answer 4";
		question.answers = [NSMutableArray arrayWithObjects:a1, a2, a3, a4, nil]; 
		[self.survey addObject:question];
		[a1 release];
		[a2 release];
		[a3 release];
		[a4 release];
	}
	// NSLog(@"Survey = %@",self.survey);
}

-(void) dealloc {
	[_survey release];
	[super dealloc];
}




@end
